package com.jysz.certificate.service.cm;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.cm.CertificateDao;
import com.jysz.certificate.persistence.enums.CertificateStatus;
import com.jysz.certificate.persistence.enums.WorkflowType;
import com.jysz.certificate.persistence.model.cm.Certificate;
import com.jysz.certificate.persistence.model.cm.CertificationDTO;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.service.workflow.WorkflowService;
import com.jysz.certificate.utils.CommonUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class CertificateService extends
		EntityService<Certificate, Long, CertificateDao> {

	@Resource
	private CertificateDao certificateDao;
	@Resource
	private WorkflowService workflowService;
	@Override
	public CertificateDao getEntityDao() {
		return certificateDao;
	}

	public Certificate getCertificateById(Long id) {
		return certificateDao.getCertificateById(id);
	}

	public Certificate getUpdateCertificate(Long id, CertificateStatus status) {
		return certificateDao.getUpdateCertificate(id, status);
	}

	public void addCertificate(Certificate c) {
		this.save(c);
	}

	public boolean getCertifite(Certificate cer) {
		return certificateDao.getCertifite(cer);
	}

	public Page<Certificate> findPage(CertificationDTO cf, int pageNo,
			int pageSize,int status) {
		Criteria criteria = getEntityDao().createCriteria();
		int a = 0;
		// 添加权限过滤
		if (cf != null && cf.getShop() != null&&CommonUtil.isNotEmpty(cf.getShop().getShopName())) {
			certificateDao.addShopCriteria(criteria, true, null);
		} else {
			certificateDao.addShopCriteria(criteria, false, null);
		}
		// criteria.addOrder(Order.asc(Certificate.SHOP));
		// criteria.addOrder(Order.desc(Certificate.DEPARTDATE));
		if (cf != null) {
			// 金融机构
			if (CommonUtil.isNotEmpty(cf.getFinancial().getName()))
				criteria.createCriteria("financial").add(
						Restrictions.like("name", cf.getFinancial().getName(),
								MatchMode.ANYWHERE));
			// 主机厂
			if (CommonUtil.isNotEmpty(cf.getBrand().getName())) {
				criteria.createCriteria("brand").add(
						Restrictions.like("name", cf.getBrand().getName(),
								MatchMode.ANYWHERE));
			}
			// 店名
			if (CommonUtil.isNotEmpty(cf.getShop().getShopName())) {
				criteria.createCriteria("shop").add(
						Restrictions.like("shopName", cf.getShop()
								.getShopName(), MatchMode.ANYWHERE));
			}
			// 合格证号
			if (CommonUtil.isNotEmpty(cf.getCertificationNum()))
				criteria.add(Restrictions.like("certificationNum",
						cf.getCertificationNum(), MatchMode.ANYWHERE));
			// 车架号
			if (CommonUtil.isNotEmpty(cf.getChassis()))
				criteria.add(Restrictions.like("chassis", cf.getChassis(),
						MatchMode.ANYWHERE));
			// 出门日
			if (cf.getGoingOutDateBegin() != null)
				criteria.add(Restrictions.ge("goingOutDate",
						cf.getGoingOutDateBegin()));
			if (cf.getGoingOutDateEnd() != null)
				criteria.add(Restrictions.le("goingOutDate",
						cf.getGoingOutDateEnd()));
			if(status>0){
				a=1;
				criteria.add(Restrictions.eq("status",
						CertificateStatus.index(status)));
			}

		}
		if(a==0){
			criteria.add(Restrictions.ne("status", CertificateStatus.NOTSTART));
		}
		return this.findPage(criteria, pageNo, pageSize);

	}

	public Map<Long, Long> findGroupCountByShops(List<Shop> result) {
		Criteria criteria = certificateDao.createCriteria();
		criteria.setProjection(Projections.groupProperty(Certificate.SHOP));
		criteria.setProjection(Projections.rowCount());
		// criteria.add(Restrictions.eq(Certificate., value))

		for (int i = 0; i < criteria.list().size(); i++) {
			Map map = (Map) criteria.list().get(i);
			map.get("");
		}
		return (Map<Long, Long>) criteria.list();
	}

	/**
	 * 合格证补录待审核数量
	 * 
	 * @param shops
	 * @return
	 */
	public Integer waitverify(Set<Shop> shops) {
		Criteria criteria = certificateDao.createCriteria();
		criteria.setProjection(Projections.rowCount());
		criteria.add(Restrictions.in("shop", shops));
		criteria.add(Restrictions.eq("status", CertificateStatus.SUBMITED));
		
		return ((Number)criteria.uniqueResult()).intValue();
	}

	/**
	 * @param id
	 */
	public void destoryAndWorkFlow(Long id) {
		workflowService.destory(id, WorkflowType.UPDATE);
		this.destory(id);
		
	}

}
